//
//  zhutiViewController.m
//  BAOASystem
//
//  Created by michael on 2018/4/23.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "MainViewController.h"
#import "webtViewController.h"
#import "caigoulistViewController.h"
#import "threelevelViewController.h"
#import "zhutiTableViewCell5.h"
#import "zhutiTableViewCell6.h"
#import "zhutiTableViewCell4.h"
#import "zhutiTableViewCell3.h"
#import "zhutiTableViewCell1.h"
#import "gouwuchelistViewController.h"
#import "OALoginViewController.h"
#import "zhutiViewController.h"
#import "shangpinlistViewController.h"
     static NSString *CellIdentifier6 = @"Cellbendititlepic6";

@interface zhutiViewController ()

@end

@implementation zhutiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem * rightItem  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"首页"] style:UIBarButtonItemStylePlain target:self action:@selector(createNewSMSAction1)];
 self.navigationItem.rightBarButtonItem = rightItem;
     UIBarButtonItem * leftItem  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"购物车"] style:UIBarButtonItemStylePlain target:self action:@selector(createNewSMSAction2)];
 self.navigationItem.leftBarButtonItem = leftItem;
             self.navigationItem.titleView = self.searchBar;
[self loadData];
    // Do any additional setup after loading the view.
}
- (void)createNewSMSAction1{
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];

if([language isEqualToString:@"0"]){
                          [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"website"];


}else{

           [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"website"];

}


 for (UIViewController * VC in self.navigationController.viewControllers) {
        if ([VC isKindOfClass:[MainViewController class]]) {
            [self.navigationController popToViewController:VC animated:YES];
            break;
        }
    }
[self.navigationController popViewControllerAnimated:YES];
}
- (void)createNewSMSAction2{
   UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"]isEqualToString:@"Y"]&&![[DanLi sharedDanLi].stay isEqualToString:@"yes"]) {
      OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        loginVC.navigationController.navigationBar.hidden = YES;
                 self.view.window.rootViewController = navVC;

}else{
gouwuchelistViewController *vc = [[gouwuchelistViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
}
#pragma mark: getter method EVNCustomSearchBar
- (EVNCustomSearchBar *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(0, kStatusBarHeight, KScreenWidth, 30)];
//0a62ea
        _searchBar.backgroundColor = [UIColor clearColor]; // 清空searchBar的背景色
                _searchBar.textFieldColor = [UIColor colorWithHexString:@"fbf2f2"alpha:.32];
                _searchBar.textColor = [UIColor whiteColor];
    [_searchBar.cancelButton setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
//        _searchBar.iconImage = [UIImage imageNamed:@"EVNCustomSearchBar.bundle/searchImageBlue.png"];
    _searchBar.iconImage = [UIImage imageNamed:@"search"];

        _searchBar.iconAlign = EVNCustomSearchBarIconAlignCenter;
        [_searchBar setPlaceholder:NSLocalizedString(@"请输入关键字", nil)];  // 搜索框的占位符
        _searchBar.placeholderColor = [UIColor whiteColor];
        _searchBar.delegate = self; // 设置代理
        [_searchBar.textField.layer setMasksToBounds:YES];

[_searchBar.textField.layer setCornerRadius:15];//设置矩形四个圆角半径

        [_searchBar sizeToFit];
    }
    return _searchBar;
}
- (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar{

shangpinlistViewController *vc = [[shangpinlistViewController alloc]init];
if (searchBar.text.length>0){
vc.searchstring  = searchBar.text;
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
}
- (void)loadData{
//_articlelistarray
 [MBProgressHUD showHUDAddedTo:self.view animated:YES];
 NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
//          dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
          dics[@"website"] = _idstring;

    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
        dics[@"type"] = BAtype;
NSString *dicstring = [httpTool dictionaryToJson:dics];
//dicstring= [dicstring URLDecodedString];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/homepage/index?data=%@",BAip,dicstring];
[httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
  [MBProgressHUD hideHUDForView:self.view animated:YES];
_dic =  dictionary;
NSLog(@"%@",dictionary);
if ([[NSString stringWithFormat:@"%@", dictionary[@"code"]]isEqualToString:@"200"]) {
//新闻轮播
_picstring = dictionary[@"appimgUrl"];
          _articlelistarray = [NSMutableArray array];
          NSArray *array = dictionary[@"articleList"];
for (NSDictionary *dic in array) {
    [_articlelistarray addObject: dic];
}
_imagearray = [NSMutableArray array];
_titlearray = [NSMutableArray array];
for (NSDictionary *dic in _articlelistarray) {
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"defaultPicUrl"]];
    [_imagearray addObject: url];
}
if(_articlelistarray){
for (NSDictionary *dic in _articlelistarray) {
    [_titlearray addObject: dic[@"title"]];
}
}
_purchasearray = dictionary[@"purchase"];
_supplyarray = dictionary[@"supply"];

//主推商品
_allMainPushProductListarray = dictionary[@"allMainPushProductList"];
_oneLevelCategoryListarray = dictionary[@"themeJx"];

[self initview];
[_tableView reloadData];
[self.tableView headerEndRefreshing];
}else{
 [MBProgressHUD hideHUDForView:self.view animated:YES];

//            [self.view makeToast:@"暂时还没数据哦"];

}

 }andFailure:^{
  [MBProgressHUD hideHUDForView:self.view animated:YES];

         [self.tableView headerEndRefreshing];

 }];
}
- (void)initview{
//if(!_tableView){
   _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-kTopHeight-kTabBarHeight) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundView.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
// [_tableView registerNib:[UINib nibWithNibName:@"TableViewCellstyle1" bundle:nil] forCellReuseIdentifier:identifier0];
  [_tableView registerNib:[UINib nibWithNibName:@"zhutiTableViewCell6" bundle:nil] forCellReuseIdentifier:CellIdentifier6];

[self.view addSubview:_tableView];
//}
 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
//        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
//    [self.tableView headerBeginRefreshing];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      switch (section) {
        case 0:
            return 1;
            break;
        case 1:
        if(_allMainPushProductListarray != nil && ![_allMainPushProductListarray isKindOfClass:[NSNull class]] && _allMainPushProductListarray.count != 0){
        
                    return 1;
        }else{
                    return 0;
        }
        case 2:
            return 2;
        case 3:
            return _oneLevelCategoryListarray.count;
//            return 0;

        default:
            return 1;
            break;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section ==0){

return 250+10+10+20;


}
else if (indexPath.section ==1){

return (KScreenWidth-30)/4+30+10+10;
}else if (indexPath.section ==2){
if (indexPath.row ==0) {

return 120+10+40;

}else{
if([_dic[@"themeDptj"] count]>0){
return 40+120+100+40+10;

}else{
    return 40;
    }
}
}
else if (indexPath.section ==3){
NSDictionary *dic =_oneLevelCategoryListarray[indexPath.row];

if ([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"1"]) {
   NSArray *array = dic[@"cppdlist"];
if (array.count>4) {
    return ((KScreenWidth-40-1)/2+60)*2+1+50;

}else if(array.count>0){
    return (KScreenWidth-40-1)/2+60+50;

}else{
 return 40;
}

}else if ([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"2"]) {
   NSArray *array = dic[@"cppdlist"];
if (array.count>5) {
    return 40+120+100+20+10;

}else if(array.count>0){
    return 40+120;

}else{
 return 40;
}

}


else{
   NSArray *array = dic[@"cppdlist"];
if (array.count>5) {
    return 40+100+10+150;

}
else if(array.count>0){
    return 40+100;

}
else{
 return 40;
}

}
}

return 1;

}
 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 if(section==3){
 UIView *view0 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 40)];
 view0.backgroundColor = [UIColor whiteColor];
  UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(5, 10, 10, 20)];
      view1.backgroundColor = [UIColor colorWithHexString:@"0aa0f6"];
      [view0 addSubview:view1];
      UILabel *label = [[UILabel alloc]init];
          label.frame = CGRectMake(view1.right+10,10, KScreenWidth-40, 20);
      [label setFont:[UIFont systemFontOfSize:18]];
       label.text  = NSLocalizedString(@"精选产品", nil);
        label.textColor = [UIColor blackColor];
       [view0 addSubview:label];
             UIView *bomview2 = [[UIView alloc]initWithFrame:CGRectMake(0, 39, KScreenWidth, 1)];
bomview2.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
       [view0 addSubview:bomview2];

return view0;
 }else{
 return nil;
 }
 }
 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
 if(section==3){
    return 40;
    }else{
    return 0;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section ==0){
if(indexPath.row ==0){
 static NSString *CellIdentifier0 = @"Cellbendititlepic0";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, KScreenWidth-10, 200-10)];
[imageview sd_setImageWithURL:[NSURL URLWithString:_picstring]];
//if ([_type isEqualToString:@"korea"]||[_type isEqualToString:@"koreaEnglish"]) {
//    imageview.image = [UIImage imageNamed:@"韩国"];
//
//}else if([_type isEqualToString:@"malaysia"]||[_type isEqualToString:@"zhejiang"]){
//    imageview.image = [UIImage imageNamed:@"马来西亚"];
//
//}else if([_type isEqualToString:@"jilin"]||[_type isEqualToString:@"jilinEnglish"]){
//    imageview.image = [UIImage imageNamed:@"吉林"];
//
//}else if([_type isEqualToString:@"jiangxi"]||[_type isEqualToString:@"jiangxiEnglish"]){
//    imageview.image = [UIImage imageNamed:@"江西"];
//
//}else{
//    imageview.image = [UIImage imageNamed:@"捷克"];
//
//}
    [cell.contentView addSubview:imageview];

    NSArray *midarray = @[@"全部分类",@"主题馆",@"采购信息",@"友好商协会"];
    NSString *string =NSLocalizedString(@"全部分类", nil);
    NSArray *titlearray = @[NSLocalizedString(@"全部分类", nil),NSLocalizedString(@"主题馆", nil),NSLocalizedString(@"采购信息", nil),NSLocalizedString(@"友好商协会", nil)];
for (int i= 0; i<4;i++) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = 200+i;
        button.titleLabel.numberOfLines = 0;
    button.frame = CGRectMake(i*(KScreenWidth/4), imageview.bottom+10, KScreenWidth/4,50+20);
    [button setTitle:titlearray[i] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:midarray[i]] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize: 13.0];
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop
                                    imageTitleSpace:20];
   [button addTarget:self action:@selector(midaction:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.contentView addSubview:button];
    
    
}
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, imageview.bottom +70+20, KScreenWidth, 10)];
    view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
    [cell.contentView addSubview:view];
    return cell;

}else{
return nil;
}
}else if(indexPath.section ==1){
if (indexPath.row ==0) {
 static NSString *CellIdentifier1 = @"CellIdentifier1";

    zhutiTableViewCell1 *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {

        cell=[[zhutiTableViewCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    cell.array = _allMainPushProductListarray;
    return cell;


}else{
return nil;
}
}else if(indexPath.section ==2){
if (indexPath.row ==0) {
 static NSString *CellIdentifier2 = @"CellIdentifier2";

    zhutiTableViewCell2 *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {

        cell=[[zhutiTableViewCell2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    cell.supplyarray = _supplyarray;
        cell.purchasearray = _purchasearray;

    return cell;
}else{
 static NSString *CellIdentifier3 = @"CellIdentifier3";

    zhutiTableViewCell3 *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {

        cell=[[zhutiTableViewCell3 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    cell.array = _dic[@"themeDptj"];
    return cell;
}

}
else{
NSDictionary *dic =_oneLevelCategoryListarray[indexPath.row];
if ([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"1"]) {
     static NSString *CellIdentifier4 = @"Cellbendititlepic4";
       zhutiTableViewCell4 *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier4];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
if (cell==nil) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell=[[zhutiTableViewCell4 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
//cell.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
    cell.dic =dic;
    return cell;

}else if([[NSString stringWithFormat:@"%@", dic[@"sort"]]isEqualToString:@"2"]){
     static NSString *CellIdentifier5 = @"Cellbendititlepic5";
       zhutiTableViewCell5 *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier5];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
if (cell==nil) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell=[[zhutiTableViewCell5 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier5];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    cell.dic =dic;
    return cell;

}else {
   NSArray *array = dic[@"cppdlist"];
if (array.count>0) {
    zhutiTableViewCell6 * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier6];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell = [[[UINib nibWithNibName:@"zhutiTableViewCell6" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
    cell.dic =dic;
    return cell;
}else{
     static NSString *CellIdentifier7 = @"Cellbendititlepic7";
       zhutiTableViewCell5 *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier7];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
if (cell==nil) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell=[[zhutiTableViewCell5 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier7];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    cell.dic =dic;
    return cell;

}
  
}
    }
}
- (void)midaction:(UIButton *)button{
if (button.tag==200) {

threelevelViewController *vc = [[threelevelViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
if (button.tag==201) {
NSArray *array = _dic[@"themeList"];
NSMutableArray *muarray = [NSMutableArray array];

                              UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    
   for (NSDictionary *dic in array) {
     UIAlertAction *action= [UIAlertAction actionWithTitle:dic[@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//     [_tableView removeFromSuperview];

zhutiViewController *vc = [[zhutiViewController alloc]init];
_type = dic[@"template"];
_idstring = dic[@"id"];
  [[NSUserDefaults standardUserDefaults] setObject:dic[@"id"] forKey:@"website"];
  [_tableView removeFromSuperview];
[self loadData];
//[self viewDidLoad];
//[self.navigationController pushViewController:vc animated:YES];

            }];
  [alert addAction:action];

}
     UIAlertAction *action= [UIAlertAction actionWithTitle:NSLocalizedString(@"关闭", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {


            }];
  [alert addAction:action];


    
            [self presentViewController:alert animated:YES completion:nil];
                  }
if (button.tag==202) {
//zhaozcViewController *vc = [[zhaozcViewController alloc]init];
caigoulistViewController *vc = [[caigoulistViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
//    vc.id = @"7";
//    vc.titlename = @"找政策";
    [self.navigationController pushViewController:vc animated:YES];

}
if (button.tag==203) {
{

webtViewController * vc = [[webtViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
vc.url = _dic[@"fdshipurl"];
[self.navigationController pushViewController:vc animated:YES];
}
}
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
